/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package pl.droidsonroids.gif.gifsample.slice;

import pl.droidsonroids.gif.gifsample.LogUtil;
import pl.droidsonroids.gif.gifsample.ResourceTable;

import ohos.aafwk.content.Intent;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import pl.droidsonroids.gif.GifDrawable;
import pl.droidsonroids.gif.GifTextView;

import java.io.IOException;

/**
 * GifTextViewSlice
 */
public class GifTextViewSlice extends ohos.aafwk.ability.AbilitySlice {
    private static final String TAG = GifTextViewSlice.class.getName();

    private ComponentContainer rootLayout;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);

        initView();

        super.setUIContent(rootLayout);
    }

    private void initView() {
        rootLayout = (ComponentContainer) LayoutScatter.getInstance(this)
                .parse(ResourceTable.Layout_gif_text_view, null, false);

        GifDrawable gifDrawable1 = null;
        GifDrawable gifDrawable2 = null;
        GifDrawable gifDrawable3 = null;
        GifDrawable gifDrawable4 = null;
        GifDrawable background = null;
        try {
            gifDrawable1 = new GifDrawable(getResourceManager()
                .getRawFileEntry("entry/resources/rawfile/anim_flag_england.gif").openRawFileDescriptor());
            gifDrawable2 = new GifDrawable(getResourceManager()
                .getRawFileEntry("entry/resources/rawfile/anim_flag_chile.gif").openRawFileDescriptor());
            gifDrawable3 = new GifDrawable(getResourceManager()
                .getRawFileEntry("entry/resources/rawfile/anim_flag_greenland.gif").openRawFileDescriptor());
            gifDrawable4 = new GifDrawable(getResourceManager()
                .getRawFileEntry("entry/resources/rawfile/anim_flag_iceland.gif").openRawFileDescriptor());
            background = new GifDrawable(getResourceManager()
                .getRawFileEntry("entry/resources/rawfile/anim_flag_iceland.gif").openRawFileDescriptor());
        } catch (IOException e) {
            LogUtil.error(TAG, "IOException");
        }

        GifTextView text = (GifTextView) rootLayout.findComponentById(ResourceTable.Id_gif);
        text.setElements(gifDrawable1, gifDrawable2, gifDrawable3, gifDrawable4);

        GifTextView textBackground = (GifTextView) rootLayout.findComponentById(ResourceTable.Id_gif1);
        textBackground.setBackgroundElement(background);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
